package com.ruoyi.system.service.gallery.impl;

import com.ruoyi.system.domain.supe.HomeCarousel;
import com.ruoyi.system.mapper.zhuangxiumapper.HomeCarouselMapper;
import com.ruoyi.system.service.gallery.HomeCarouselService;
import com.ruoyi.system.utils.WxConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import java.util.List;

@Service
public class HomeCarouselServiceImpl implements HomeCarouselService {

    @Autowired
    private HomeCarouselMapper homeCarouselMapper;

    @Override
    public void doWxLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String authUrl = WxConstants.AUTH_BASE_URL + "appid=" + WxConstants.APPID
                + "&redirect_uri=" + URLEncoder.encode(WxConstants.REDIRECT_URL)
                + "&response_type=code"
                + "&scope=" + WxConstants.SCOPE
                + "&state=STATE#wechat_redirect";


        String signature = request.getParameter("signature");/// 微信加密签名
        String timestamp = request.getParameter("timestamp");/// 时间戳
        String nonce = request.getParameter("nonce"); /// 随机数
        String echostr = request.getParameter("echostr"); // 随机字符串

        PrintWriter out = response.getWriter();

        if (signature != null && timestamp != null && nonce != null && echostr != null) {
//            if (SignUtil.checkSignature(signature, timestamp, nonce)) {
//                out.print(echostr);
//            }
            out.close();
        } else {
            response.sendRedirect(authUrl);
        }

    }


    /**
     * 查询首页轮播图
     *
     * @param carouselId 首页轮播图ID
     * @return 首页轮播图
     */
    @Override
    public HomeCarousel selectHomeCarouselById(Long carouselId)
    {
        return homeCarouselMapper.selectHomeCarouselById(carouselId);
    }

    /**
     * 查询首页轮播图列表
     *
     * @param homeCarousel 首页轮播图
     * @return 首页轮播图
     */
    @Override
    public List<HomeCarousel> selectHomeCarouselList(HomeCarousel homeCarousel)
    {
        return homeCarouselMapper.selectHomeCarouselList(homeCarousel);
    }

    /**
     * 新增首页轮播图
     *
     * @param homeCarousel 首页轮播图
     * @return 结果
     */
    @Override
    public int insertHomeCarousel(HomeCarousel homeCarousel)
    {
        return homeCarouselMapper.insertHomeCarousel(homeCarousel);
    }

    /**
     * 修改首页轮播图
     *
     * @param homeCarousel 首页轮播图
     * @return 结果
     */
    @Override
    public int updateHomeCarousel(HomeCarousel homeCarousel)
    {
        return homeCarouselMapper.updateHomeCarousel(homeCarousel);
    }

    /**
     * 批量删除首页轮播图
     *
     * @param carouselIds 需要删除的首页轮播图ID
     * @return 结果
     */
    @Override
    public int deleteHomeCarouselByIds(Long[] carouselIds)
    {
        return homeCarouselMapper.deleteHomeCarouselByIds(carouselIds);
    }

    /**
     * 删除首页轮播图信息
     *
     * @param carouselId 首页轮播图ID
     * @return 结果
     */
    @Override
    public int deleteHomeCarouselById(Long carouselId)
    {
        return homeCarouselMapper.deleteHomeCarouselById(carouselId);
    }

    @Override
    public List<HomeCarousel> findlistArea(String carouselArea) {
        return homeCarouselMapper.findlistArea(carouselArea);
    }

}
